package com.white.mysoundrecorder;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by yutf on 2017/8/30 0030.
 */

public class RecordingService extends Service
{

    MyDbHelper helper;
    MediaRecorder recorder;
    String parentPath;

    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    {
        return new MyBinder();
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        helper = new MyDbHelper(getApplicationContext());
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId)
    {
        Notification notification = new Notification(android.R.drawable.ic_lock_lock, "", System.currentTimeMillis());
        startForeground(0, notification);
        return super.onStartCommand(intent, flags, startId);
    }

    public class MyBinder extends IMyAidlInterface.Stub
    {
        private RecordItem currentItem;
        private String recordPath;
        private String fileName;
        private long startTimestamp;

        @Override
        public void start(String path) throws RemoteException
        {
            try
            {
                startTimestamp = System.currentTimeMillis();
                fileName = "record_" + startTimestamp;
                recordPath = path + "/" + fileName + ".mp4";

                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                recorder.setAudioChannels(1);
                recorder.setOutputFile(recordPath);
                recorder.setAudioSamplingRate(44100);
                recorder.setAudioEncodingBitRate(192000);
                recorder.prepare();
                recorder.start();
                if(callback != null)
                {
                    callback.onStartRecording();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void stop() throws RemoteException
        {
            if(recorder != null)
            {
                recorder.stop();
                recorder.release();
                recorder = null;
                if(callback != null)
                {
                    callback.onStop();
                }
                if(!TextUtils.isEmpty(recordPath))
                {
                    File f = new File(recordPath);
                    if(f.exists())
                    {
                        save(f);
                        if(callback != null)
                        {
                            callback.onSuccess(recordPath);
                        }
                    }
                }
            }
        }

        private void save(File f)
        {
            currentItem = new RecordItem();
            currentItem.name = fileName;
            currentItem.id = null;
            currentItem.filePath = recordPath;
            currentItem.addTime = startTimestamp;
            long now = System.currentTimeMillis();
            currentItem.duration = now - startTimestamp;
            currentItem.fileSize = f.length();
            helper.addItem(currentItem);
        }

        @Override
        public void registCallback(IRecordCallback c) throws RemoteException
        {
            callback = c;
        }

    }

    IRecordCallback callback;

    @Override
    public void onDestroy()
    {
        //先关闭前台状态
        stopForeground(true);
        super.onDestroy();
    }
}
